package com.wqh.zeromq.demo;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Logger;

import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;

/**
 * 使用见简单的请求-应答模式
 * 请求必须是客户端发起,服务端响应s
 */

public class HWClient {

    private static Logger logger = Logger.getLogger(HWClient.class.getSimpleName());

    public static void main(String[] args) {
        logger.info("正在连接至Hello World服务端......");
        //初始化ZMQ连接器
        try(ZContext context = new ZContext()){
            ZMQ.Socket socket = context.createSocket(SocketType.REQ);
            //判断与服务端的连接是否成功
            if(socket.connect("tcp://localhost:5555")){

                BufferedReader reader =
                        new BufferedReader(new InputStreamReader(System.in));
                System.out.print("<<");
                String line;
                while((line = reader.readLine()) != null){
                    //客户端开始回应服务器端
                    socket.send(line.getBytes(ZMQ.CHARSET),0);

                    byte[] reply = socket.recv(0);
                    System.out.println(">>"+new String(reply,ZMQ.CHARSET));
                    System.out.print("<<");
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
